Imports TuitcAutoInfo.BLL

Public Class DivisionPress
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents txtKeywords As System.Web.UI.WebControls.TextBox
    Protected WithEvents lstYear As System.Web.UI.WebControls.DropDownList
    Protected WithEvents lstMonth As System.Web.UI.WebControls.DropDownList
    Protected WithEvents tblArticles As System.Web.UI.WebControls.Table
    Protected WithEvents lblHeading As System.Web.UI.WebControls.Label
    Protected WithEvents lstCategory As System.Web.UI.WebControls.DropDownList
    Protected WithEvents btnSearch As System.Web.UI.WebControls.Button
    Protected WithEvents PagingHandler1 As PagingHandler

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Const MAX_PAGEITEMS = 5

    Dim categoryID As Integer
    Dim SolutionID As Integer
    Dim dt As DataTable

    Dim MenuID As Integer
    'Dim CurrentPage As Integer

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'PagingHandler1.CurrentPage = CInt(viewstate("CurrentPage"))

        If Request.QueryString("categoryID") <> String.Empty Then
            viewstate("categoryID") = Request.QueryString("categoryID")
            categoryID = viewstate("categoryID")
        End If

        If Request.QueryString("solutionID") <> String.Empty Then
            viewstate("solutionID") = Request.QueryString("solutionID")
            SolutionID = viewstate("solutionID")
        End If

        MenuID = Request("menuid")
        If Not Page.IsPostBack Then
            bindListCategory()
            bindListYear()
            bindListMonth()
            lblHeading.Text = GetMenuText(MenuID)

            dt = GetData("", SolutionID, categoryID, 0, 0)
        End If
        
    End Sub

    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
        'PagingHandler1.CurrentPage = CInt(viewstate("CurrentPage"))

        If dt Is Nothing Then dt = GetData("", SolutionID, categoryID, 0, 0)

        With PagingHandler1
            .DataSource = dt
            .PageSize = MAX_PAGEITEMS
            .bindControl()
        End With
        DrawListTable(dt)

        'viewstate("CurrentPage") = PagingHandler1.CurrentPage
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        categoryID = lstCategory.SelectedValue
        viewstate("categoryID") = categoryID
        dt = GetData(txtKeywords.Text, SolutionID, categoryID, lstYear.SelectedValue, lstMonth.SelectedValue)
        PagingHandler1.Reset()
    End Sub

    Private Function GetData(ByVal Keywords As String, ByVal solID As Integer, ByVal catID As Integer, ByVal Y As String, ByVal M As String) As DataTable
        Dim objArticle As New Article
        Return objArticle.SelectByCriteria(Keywords, solID, catID, Y, M)
    End Function


    Private Sub bindListCategory()
        Dim objcat As New BLL.Category
        Dim dt As DataTable
        Dim dr As DataRow

        dt = objcat.SelectAll()
        lstCategory.Items.Add(New ListItem("", 0))
        For Each dr In dt.Rows
            lstCategory.Items.Add(New ListItem(dr("Name"), dr("ID")))
        Next

        If categoryID > 0 Then lstCategory.SelectedValue = categoryID
    End Sub

    Private Sub bindListYear()
        lstYear.Items.Add(New ListItem("-Year-", 0))
        For i As Integer = 2000 To 2010
            lstYear.Items.Add(New ListItem(i, i))
        Next
    End Sub

    Private Sub bindListMonth()
        lstMonth.Items.Add(New ListItem("-Month-", 0))
        lstMonth.Items.Add(New ListItem("Jan", 1))
        lstMonth.Items.Add(New ListItem("Feb", 2))
        lstMonth.Items.Add(New ListItem("Mar", 3))
        lstMonth.Items.Add(New ListItem("Apr", 4))
        lstMonth.Items.Add(New ListItem("May", 5))
        lstMonth.Items.Add(New ListItem("Jun", 6))
        lstMonth.Items.Add(New ListItem("Jul", 7))
        lstMonth.Items.Add(New ListItem("Aug", 8))
        lstMonth.Items.Add(New ListItem("Sep", 9))
        lstMonth.Items.Add(New ListItem("Oct", 10))
        lstMonth.Items.Add(New ListItem("Nov", 11))
        lstMonth.Items.Add(New ListItem("Dec", 12))
    End Sub

    Private Sub DrawListTable(ByVal data As DataTable)
        Dim tr As New TableRow
        Dim td As New TableCell
        Dim dr As DataRow
        Dim ArticleSource As String

        Dim img As CMSCore.RWControls.RWResizedImage

        td.ColumnSpan = 2
        td.Text = "<hr>"
        tr.Cells.Add(td)
        tblArticles.Rows.Add(tr)

        For i As Integer = PagingHandler1.CurrentPage * MAX_PAGEITEMS To _
            MAX_PAGEITEMS * (PagingHandler1.CurrentPage + 1) - 1
            If i < dt.Rows.Count Then

                dr = data.Rows(i)
                ArticleSource = "" & dr("Source")
                ArticleSource = Trim(ArticleSource)
                If ArticleSource = String.Empty Then
                    ArticleSource = "Unavailable"
                End If

                tr = New TableRow
                td = New TableCell
                td.VerticalAlign = VerticalAlign.Top
                td.Width = New System.Web.UI.WebControls.Unit("0%")

                '// Image
                If dr("Thumbnail") <> String.Empty Then
                    img = New CMSCore.RWControls.RWResizedImage
                    img.Width = New System.Web.UI.WebControls.Unit("100px")
                    img.MaxHeight = New System.Web.UI.WebControls.Unit("100px")
                    img.ThumbnailDirURL = "~/UploadedFiles/Thumbnails/"
                    img.ImageUrl = "~/UploadedFiles/" & dr("Thumbnail")
                    td.Controls.Add(img)
                Else
                    td.Text = "&nbsp;"
                End If
                tr.Cells.Add(td)
                tblArticles.Rows.Add(tr)

                '// Text
                td = New TableCell
                td.VerticalAlign = VerticalAlign.Top
                td.Width = New System.Web.UI.WebControls.Unit("70%")

                td.Text = "<table width=""100%"" border=""0"">" _
                        & "  <tr>" _
                        & "     <td><span class=""head"">" & dr("Headline") & "</span></td>" _
                        & "     <td align=""right""><span class=""italic"">Source: " & ArticleSource & "</span></td>" _
                        & "  </tr>" _
                        & "  <tr>" _
                        & "     <td colspan=""2""><br>" & Left("" & dr("Detail"), 200) & "...</td>" _
                        & "  </tr>" _
                        & "  <tr>" _
                        & "     <td align=""right"" colspan=""2""><a href=""ArticleView.aspx?menuid=" & CStr(MenuID) & "&Articleid=" & dr("ID") & """>read more &gt;&gt;</a></td>" _
                        & "  </tr>" _
                        & "</table>"
                        

                'td.Text = "<span class=""head"">" & dr("Headline") _
                '        & "</span><br>" & Left("" & dr("Detail"), 200) _
                '        & "...<br><a href=""ArticleView.aspx?menuid=" & CStr(MenuID) & "&Articleid=" & dr("ID") & """>read more &gt;&gt;</a>"
                tr.Cells.Add(td)

                tblArticles.Rows.Add(tr)

                tr = New TableRow
                td = New TableCell
                td.ColumnSpan = 2
                td.Text = "<hr>"
                tr.Cells.Add(td)
                tblArticles.Rows.Add(tr)
            Else
                Exit For
            End If
        Next
    End Sub

    Private Function GetMenuText(ByVal ID As Integer) As String
        Dim ObjMenu As New BLL.SiteMenu(ID, True)

        GetMenuText = ObjMenu.Caption
        ObjMenu = Nothing
        Exit Function
    End Function


End Class
